
#include <rtthread.h>
#include <rtdevice.h>

#define LVGL_TIMER_PERIOD     10          //ms

static void ez_lvgl_timer_cb(void *param)
{
    lv_tick_inc(LVGL_TIMER_PERIOD);
}

void ez_lvgl_main_task(void *param)
{
    lv_theme_set_current(lv_theme_default_init(200, NULL));

    while (1)
    {
        rt_thread_delay(rt_tick_from_millisecond(LVGL_TIMER_PERIOD));
        lv_task_handler();
    }
}

int ez_lvgl_start(void)
{
    /* littlevgl Init */
    lv_init();
#if USE_LV_LOG
    /* littlevgl Log Init */
    lv_log_register_print(lvgl_rtt_log_register);
#endif

    lv_port_disp_init();

    //init a timer for lv_tick
    {
        static struct rt_timer lv_timer = { 0 };

        rt_timer_init(&lv_timer,
                      "lv_tmr",
                      ez_lvgl_timer_cb,
                      RT_NULL,
                      rt_tick_from_millisecond(LVGL_TIMER_PERIOD),
                      RT_TIMER_FLAG_PERIODIC);
        rt_timer_start(&lv_timer);
    }

    //create a task for lv
    {
        rt_thread_t tid;

        tid = rt_thread_create("lv_main", ez_lvgl_main_task, RT_NULL, 2048, 8, 10);
        if (tid)
        {
            rt_thread_startup(tid);
        }
    }


    return RT_EOK;
}
//INIT_ENV_EXPORT(ez_lvgl_start);

